﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Conexion;
using System.Net.Sockets;
using System.IO;
using System.Windows.Forms;

namespace Cliente
{
    class BroadcastHandler
    {
        private Config conf = null;
        private const string CMD_RAM = "01";
        private const string CMD_CPU = "02";
        private const string CMD_DISCO = "03";
        private const string CMD_SERV = "04";
        private const string HDR_BRO = "BRO";
        public ClientForm ventana = null;
        private Thread th;

        public BroadcastHandler(Config conf, ClientForm form)
        {
            this.conf = conf;
            this.ventana = form;
        }

        public void IniciarServicio() {

            th = new Thread(EscucharBroadcast);
            th.IsBackground = true;
            th.Start();
        }
       
        private void EscucharBroadcast()
        {
            UDPCLIENTE cliente = new UDPCLIENTE(conf.Ip_broadcast, conf.Puerto_broadcast);
            string msjBroadcast = "";
            char[] cero = new char[] { '0' };
            while (true)
            {
                msjBroadcast = cliente.Recive();
                string CantServidores = "";
                string ramTotal = "";
                string ramDisponible = "";
                string usoCpu = "";
                string discoTotal = "";
                string discoDisponible = "";
                int indice = 0;

                string hdr = msjBroadcast.Substring(indice, 3);
                indice += 3;
                if (hdr.Equals(HDR_BRO))
                {
                    while (indice < msjBroadcast.Length)
                    {
                        string cmd = msjBroadcast.Substring(indice, 2);
                        indice += 2;
                        switch (cmd)
                        {
                            case CMD_RAM:
                                ramTotal = msjBroadcast.Substring(indice, 15);
                                indice += 15;
                                ramDisponible = msjBroadcast.Substring(indice, 15);
                                indice += 15;
                                break;
                            case CMD_DISCO:
                                discoTotal = msjBroadcast.Substring(indice, 15);
                                indice += 15;
                                discoDisponible = msjBroadcast.Substring(indice, 15);
                                indice += 15;
                                break;
                            case CMD_CPU:
                                usoCpu = msjBroadcast.Substring(indice, 15);
                                indice += 15;
                                break;
                            case CMD_SERV:
                                CantServidores = msjBroadcast.Substring(indice, 1);
                                indice += 1;
                                break;
                            default:
                                Console.WriteLine("Default case");
                                break;
                        }
                    }

                }
                CantServidores = CantServidores.TrimStart(cero);
                if (string.IsNullOrEmpty(CantServidores))
                    CantServidores = "0";

                usoCpu = usoCpu.TrimStart(cero);
                if (string.IsNullOrEmpty(usoCpu))
                    usoCpu = "0";

                ramDisponible = ramDisponible.TrimStart(cero);
                if (string.IsNullOrEmpty(ramDisponible))
                    ramDisponible = "0";

                ramTotal = ramTotal.TrimStart(cero);
                if (string.IsNullOrEmpty(ramTotal))
                    ramTotal = "0";

                discoTotal = discoTotal.TrimStart(cero);
                if (string.IsNullOrEmpty(discoTotal))
                    discoTotal = "0";

                discoDisponible = discoDisponible.TrimStart(cero);
                if (string.IsNullOrEmpty(discoDisponible))
                    discoDisponible = "0";

                SetText((Label)ObtenerControl("lblResCantServers"), CantServidores);
                SetText((Label)ObtenerControl("lblResCpu"), usoCpu);
                SetText((Label)ObtenerControl("lblResRamDisp"), ramDisponible + " MB");
                SetText((Label)ObtenerControl("lblResRamTotal"), ramTotal + " MB");
                SetText((Label)ObtenerControl("lblResDisco"), discoTotal + " GB");
                SetText((Label)ObtenerControl("lblResDiscoDisp"), discoDisponible + " GB");
                
            }
        }


        private Control ObtenerControl(string cmd)
        {
            Control[] controles = ventana.Controls.Find(cmd, true);

            return controles[0];
        }

        delegate void SetTextCallback(Label ctrl, string text);

        private void SetText(Label ctrl, string text)
        {
           

            if (ctrl.InvokeRequired)
            {
                ctrl.BeginInvoke(new SetTextCallback(SetText), ctrl, text);
            }
            else
            {
                ctrl.Text = text;
            }
        }

    }
}
